package com.gjw.planeWar;

import com.gjw.style.Style;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * @author gjw19
 * @date 2019/5/2
 */
public class PlaneFrame extends Frame {

    private static final long serialVersionUID = -9201937184302579896L;

    GameModel gm = GameModel.getInstance();
    Props props = Props.getInstance();

    public PlaneFrame() {
        // 设置窗口的高宽
        this.setSize(props.GAME_WIDTH, props.GAME_HEIGHT);
        // 设置窗口标题
        this.setTitle("plane war");
        // 窗口是否可以改变尺寸
        this.setResizable(false);
        // 窗口是否可见
        this.setVisible(true);
        // 添加键盘监听事件
        this.addKeyListener(new MyKeyListener());
        this.addWindowListener(new WindowAdapter() {
            // 窗口可以关闭按钮进行关闭
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }

    // 解决闪烁问题，固定套路
    Image offScreenImage = null;

    @Override
    public void update(Graphics g) {
        if (offScreenImage == null) {
            offScreenImage = this.createImage(props.GAME_WIDTH, props.GAME_HEIGHT);
        }
        Graphics gOffScreen = offScreenImage.getGraphics();
        Color c = gOffScreen.getColor();
        gOffScreen.setColor(Color.BLACK);
        gOffScreen.fillRect(0, 0, props.GAME_WIDTH, props.GAME_HEIGHT);
        gOffScreen.setColor(c);
        paint(gOffScreen);
        g.drawImage(offScreenImage, 0, 0, null);
    }

    @Override
    public void paint(Graphics g) {
        gm.paint(g);
        if (!gm.heroPlane.isLiving()) {
            showDearhNews(g);
            return;
        }
    }

    class MyKeyListener extends KeyAdapter {
        boolean bL = false;
        boolean bU = false;
        boolean bR = false;
        boolean bD = false;

        // 按下
        @Override
        public void keyPressed(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_LEFT:
                    bL = true;
                    break;
                case KeyEvent.VK_UP:
                    bU = true;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = true;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = true;
                    break;
                case KeyEvent.VK_F1:
                    Style.getINSTANCE().changeStyle(1);
                    break;
                case KeyEvent.VK_F2:
                    Style.getINSTANCE().changeStyle(2);
                    break;
                default:
                    break;
            }
            setMainHeroPlaneDir();
        }

        // 松开
        @Override
        public void keyReleased(KeyEvent e) {
            int key = e.getKeyCode();
            switch (key) {
                case KeyEvent.VK_LEFT:
                    bL = false;
                    break;
                case KeyEvent.VK_UP:
                    bU = false;
                    break;
                case KeyEvent.VK_RIGHT:
                    bR = false;
                    break;
                case KeyEvent.VK_DOWN:
                    bD = false;
                    break;
                case KeyEvent.VK_A:
                    if (!gm.heroPlane.isLiving()) {
                        gm.init();
                    }
                default:
                    break;
            }
            setMainHeroPlaneDir();
        }

        // 设置英雄飞机的方向
        private void setMainHeroPlaneDir() {
            if (!bL && !bU && !bR && !bD) {
                // 如果未按过方向键，就把其移动标志设为false
                gm.heroPlane.setHerodir(HeroDir.STOP);
            } else {
                if (bL) {
                    gm.heroPlane.setHerodir(HeroDir.LEFT);
                }
                if (bU) {
                    gm.heroPlane.setHerodir(HeroDir.UP);
                }
                if (bR) {
                    gm.heroPlane.setHerodir(HeroDir.RIGHT);
                }
                if (bD) {
                    gm.heroPlane.setHerodir(HeroDir.DOWN);
                }
            }
        }
    }

    // 展示死亡消息
    private void showDearhNews(Graphics g) {
        Color color = g.getColor();
        g.drawImage(Style.getINSTANCE().gameOver, 0, 0, null);
        g.setColor(Color.BLUE);
        g.setFont(new Font(null, 0, 20));
        g.drawString("第" + props.LEVEL + "关", 175, 60);
        g.setColor(Color.YELLOW);
        g.setFont(new Font(null, 0, 15));
        g.drawString("得分 " + props.SCORE, 173, 85);
        g.setColor(Color.WHITE);
        g.setFont(new Font(null, 0, 20));
        g.drawString("击落敌军数 " + props.SHOOT_ENEMY_NUM, props.GAME_WIDTH / 2 - 60, props.GAME_HEIGHT / 2 + 50);
        g.setColor(Color.RED);
        g.setFont(new Font(null, 0, 25));
        g.drawString("按A键重新游戏", props.GAME_WIDTH / 2 - 80, props.GAME_HEIGHT / 2 + 100);
        g.setColor(color);
    }

}
